package com.atguigu.tingshu.user.pattern.factory;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.user.pattern.DeliveryStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author 饭勺
 * @description 自动发现所有策略实现类对象，对外提供一个方法根据购买项目类型返回策略实现类对象
 * @createDate 2025/10/18 16:40
 */
@Slf4j
@Component
public class StrategyFactory {

    @Autowired
    private List<DeliveryStrategy> strategyList;


    /**
     * 自动将Map中Value类型（策略接口）注入所有实现类对象注入
     * Map中Key策略实现类Bean名称
     * Map中Value策略接口实现类对象
     */
    @Autowired
    private Map<String,DeliveryStrategy> strategyMap;


    /**
     * 根据购买项目类型获取策略实现类对象
     * @param itemType
     * @return
     */
    public DeliveryStrategy getDeliveryStrategy(String itemType) {
        //根据购买项目类型获取策略实现类对象
        if (strategyMap.containsKey(itemType)) {
            return strategyMap.get(itemType);
        }
        throw new GuiguException(500,"参数有误");
    }
}
